/***********************************************************
Copyright(C), Yk Technology
FileName	: main.c
Author		: Yukewantong, Zhongxiaodong
Date	  	: 2011/12/22
Description	: 
Version		: V1.0
History		: 
--------------------------------
2011/12/22: created
2012/3/13: transplat to Siliconlab platfrom
***********************************************************/
#include "stm32f1xx_hal.h"
#define EPD_W21_SPI_SPEED 0x02

//#define EPD_W21_VPP_0	P2 &= ~0x10
//#define EPD_W21_VPP_1	P2 |= 0x10

//#define EPD_W21_MOSI_0	P2 &= ~0x08
#define EPD_W21_MOSI_0 HAL_GPIO_WritePin(EPD_SDA_GPIO_Port, EPD_SDA_Pin, GPIO_PIN_RESET)
//#define EPD_W21_MOSI_1	P2 |= 0x08
#define EPD_W21_MOSI_1 HAL_GPIO_WritePin(EPD_SDA_GPIO_Port, EPD_SDA_Pin, GPIO_PIN_SET)

//#define EPD_W21_CLK_0	P2 &= ~0x04
#define EPD_W21_CLK_0 HAL_GPIO_WritePin(EPD_SCK_GPIO_Port, EPD_SCK_Pin, GPIO_PIN_RESET)
//#define EPD_W21_CLK_1	P2 |= 0x04
#define EPD_W21_CLK_1 HAL_GPIO_WritePin(EPD_SCK_GPIO_Port, EPD_SCK_Pin, GPIO_PIN_SET)

//#define EPD_W21_CS_0	P2 &= ~0x01
#define EPD_W21_CS_0 HAL_GPIO_WritePin(EPD_CS_GPIO_Port, EPD_CS_Pin, GPIO_PIN_RESET)
//#define EPD_W21_CS_1	P2 |= 0x01
#define EPD_W21_CS_1 HAL_GPIO_WritePin(EPD_CS_GPIO_Port, EPD_CS_Pin, GPIO_PIN_SET)

//#define EPD_W21_DC_0	P2 &= ~0x02
#define EPD_W21_DC_0 HAL_GPIO_WritePin(EPD_DC_GPIO_Port, EPD_DC_Pin, GPIO_PIN_RESET)
//#define EPD_W21_DC_1	P2 |= 0x02
#define EPD_W21_DC_1 HAL_GPIO_WritePin(EPD_DC_GPIO_Port, EPD_DC_Pin, GPIO_PIN_SET)

//#define EPD_W21_RST_0	P2 &= ~0x40
#define EPD_W21_RST_0 HAL_GPIO_WritePin(EPD_RES_GPIO_Port, EPD_RES_Pin, GPIO_PIN_RESET)
//#define EPD_W21_RST_1	P2 |= 0x40
#define EPD_W21_RST_1 HAL_GPIO_WritePin(EPD_RES_GPIO_Port, EPD_RES_Pin, GPIO_PIN_SET)

#define EPD_W21_BS_0 HAL_GPIO_WritePin(EPD_RES_GPIO_Port, EPD_BSI_Pin, GPIO_PIN_RESET)
#define EPD_W21_BS_1 HAL_GPIO_WritePin(EPD_RES_GPIO_Port, EPD_BSI_Pin, GPIO_PIN_SET)

#define MFCSB_L  HAL_GPIO_WritePin(EPD_MFCSB_GPIO_Port, EPD_MFCSB_Pin, GPIO_PIN_RESET)
#define MFCSB_H  HAL_GPIO_WritePin(EPD_MFCSB_GPIO_Port, EPD_MFCSB_Pin, GPIO_PIN_SET)
//#define MFDO  P2OUT_bit.P2OUT0
#define MFDO 	HAL_GPIO_ReadPin(EPD_MFDO_GPIO_Port, EPD_MFDO_Pin)
#define BUSYN HAL_GPIO_ReadPin(EPD_BUSY_GPIO_Port, EPD_BUSY_Pin)
#define SDA_IN HAL_GPIO_ReadPin(EPD_SDA_GPIO_Port, EPD_SDA_Pin)

#define RSTN_L EPD_W21_RST_0
#define RSTN_H EPD_W21_RST_1

#define SDA_L EPD_W21_MOSI_0
#define SDA_H EPD_W21_MOSI_1

#define SCL_L EPD_W21_CLK_0
#define SCL_H EPD_W21_CLK_1

#define CSB_L EPD_W21_CS_0
#define CSB_H EPD_W21_CS_1

#define 	BS_L EPD_W21_BS_0
#define   BS_H EPD_W21_BS_1

#define lut_None NULL
#define DATA_MASK   0x0100
#define DCX_CMD     0x0000
#define DCX_DATA    0x0001
#define MASTER      0x00
#define SLAVE       0x01
#define MAS_SLA     0x02
#define PSR         0x0000
#define EPDPWR         0x0001
#define POF         0x0002
#define PFS         0x0003
#define PON         0x0004

#define BTST        0x0006
#define DSLP        0x0007
#define DTM1        0x0010
#define DSP         0x0011
#define DRF         0x0012

#define LUT_VCOM1   0x0020
#define LUT_WHITE   0x0022
#define LUT_BLACK   0x0021
#define LUT_GRAY1   0x0023
#define LUT_GRAY2   0x0024
#define LUT_RED0    0x0025
#define LUT_RED1    0x0026
#define LUT_RED2    0x0027
#define LUT_RED3    0x0028
#define LUT_XON     0x0029
#define EPDPLL         0x0030
#define TSC         0x0040
#define TSE         0x0041
#if 1
#define CDI         0x0050
#define LPD         0x0051
#define TCON        0x0060
#define TRES        0x0061
#define DAM         0x0065
#define REV         0x0070
#define FLG         0x0071
#define AMV         0x0080
#define VV          0x0081
#define VDCS        0x0082


#define Image_Start  0x003000

#define DEMO_KIT         0x00
#define RA_JIG           0x01
#define TEST_JIG         0x02
#define DEMO_KIT_AV      0x10
#define RA_JIG_AV        0x11
#define TEST_JIG_AV      0x12


#define Initial_Counter            64
#define Temperature_LUT_Counter   1024
#if 1
#ifdef __UC8159_C__
#define __UC8159_EXTERN__
#else 
#define __UC8159_EXTERN__ extern
#endif

__UC8159_EXTERN__ void EPD_Init(void);
__UC8159_EXTERN__ void EPD_Load_LUT(unsigned int LUT, unsigned char const *LUT_Value);
__UC8159_EXTERN__ void CSB_Low(void);
__UC8159_EXTERN__ void CSB_High(void);
__UC8159_EXTERN__ void SPI_COMMAND(unsigned char dat);
__UC8159_EXTERN__ void SPI_DATA(unsigned char dat);
__UC8159_EXTERN__ void spi_9b_send_9b(unsigned int dat);
__UC8159_EXTERN__ void spi_9b_send(unsigned int dcx,unsigned char dat);
__UC8159_EXTERN__ unsigned char SPI_GET_DATA(void);
__UC8159_EXTERN__ void CLEAR_LUT(void);
__UC8159_EXTERN__ void Upload_Temperature_LUT(void);
__UC8159_EXTERN__ void EPD_Display_White_Flash(void);
__UC8159_EXTERN__ void EPD_Display_White(void);

__UC8159_EXTERN__ void EPD_Display_Black(void);
__UC8159_EXTERN__ void EPD_Display_Black_Flash(void);

__UC8159_EXTERN__ void EPD_Display_chess(void);



__UC8159_EXTERN__ void EPD_Display_Img(unsigned char * imgData );
__UC8159_EXTERN__ void EPD_Display_Img1(void);
__UC8159_EXTERN__ void EPD_Display_Img2(void);

__UC8159_EXTERN__ void EPD_Display_Red_Flash(void);
__UC8159_EXTERN__ void EPD_Display_White_and_Black(void);
__UC8159_EXTERN__ void Send_Image_Data_KW(unsigned char const *img_kw);
__UC8159_EXTERN__ void check_busy_high(void);// If BUSYN=0 then waiting
__UC8159_EXTERN__ void check_busy_low(void);// If BUSYN=1 then waiting
__UC8159_EXTERN__ void setPON(unsigned char Flag);
__UC8159_EXTERN__ void setPOF(unsigned char Flag);
__UC8159_EXTERN__ void send_SPI_DATA(unsigned char *data, unsigned int counter);

__UC8159_EXTERN__ long int IMAGE_COUNTER;
__UC8159_EXTERN__ long int IMAGE_SIZE;
__UC8159_EXTERN__ unsigned int long FLASH_IMAGE_COUNTER;
__UC8159_EXTERN__ unsigned char PON_flag;



__UC8159_EXTERN__ void power_saving(void);
__UC8159_EXTERN__ void out_of_power_saving(void);

__UC8159_EXTERN__ unsigned char get_FPC_flash_pll_data(void);


#endif
#endif
/***********************************************************
						end file
***********************************************************/
